"""
date_sub 格式处理
"""

import re
import sys
import os
import logging
import pandas as pd


def change_cur_date_sub_date(m):
    new_date_format = "TO_CHAR(CURRENT_DATE - INTERVAL '{} DAY', 'yyyyMMdd')::timestamp".format(
        str(m.group(5)).replace("'", "").replace("-", "").strip())
    return new_date_format


def change_cur_date_sub_date_2(m):
    logging.info("change_cur_date_sub_date_2")
    logging.debug(m)
    new_date_format = "TO_CHAR(CURRENT_DATE - INTERVAL '{} DAY', 'yyyy-MM-dd')::timestamp".format(
        str(m.group(5)).replace("'", "").strip())
    return new_date_format


def change_cur_date_sub_date_3(m):
    logging.info("change_cur_date_sub_date_3")
    logging.debug(m)
    new_date_format = "TO_CHAR(CURRENT_DATE - INTERVAL '{} MONTH', 'yyyy-MM-dd')::timestamp".format(
        str(m.group(5)).replace("'", "").strip())
    return new_date_format


def change_cur_date_sub_month_date(m):
    new_date_format = "TO_CHAR(CURRENT_DATE - INTERVAL '{} MONTH', 'yyyy-MM-dd')::timestamp".format(
        str(m.group(5)).replace("'", "").strip())
    return new_date_format


def change_date_sub_minute(m):
    logging.info("change_date_sub_minute")
    new_date_format = " now() - interval '{} MINUTE'".format(str(m.group(4)).replace("'", "").strip())
    return new_date_format


def rep_complex_fn_date_sub(document_content):
    logging.info("rep_complex_fn_date_sub")
    # date_sub(date_format(now(),'%Y%m%d'),interval 120 DAY)
    date_sub_patten = re.compile(r'(DATE_SUB|date_sub)\((DATE_FORMAT|date_format)\((NOW|now)\(\)\,\'\%Y\%m\%d\'\)\,(INTERVAL|interval)(.*)(DAY|day)\)')
    result = date_sub_patten.sub(change_cur_date_sub_date, document_content)
    document_content = result

    # date_sub(date_format(now(), '%Y-%m-%d'), interval 100 day)
    # date_sub(date_format(now(), '%Y-%m-%d'), interval 100 day)
    # date_sub\(date_format\(now\(\)\, \'\%Y\-\%m\-\%d\'\)\,interval 30 DAY\)

    date_sub_patten = re.compile(
        r"(DATE_SUB|date_sub)\((DATE_FORMAT|date_format)\((NOW|now)\(\)\,'%Y-%m-%d'\)\,(INTERVAL|interval)(.*)(DAY|day)\)")
    result = date_sub_patten.sub(change_cur_date_sub_date_2, document_content)
    document_content = result

    date_sub_patten = re.compile(
        r"(DATE_SUB|date_sub)\((DATE_FORMAT|date_format)\((NOW|now)\(\)\,'%Y-%m-%d'\)\,(INTERVAL|interval)(.*)(MONTH|month)\)")
    result = date_sub_patten.sub(change_cur_date_sub_date_3, document_content)
    document_content = result

    # substring\(date_sub\(now\(\)\,INTERVAL 6 MONTH\)\,1\,11\)
    date_sub_patten = re.compile(
        r"(SUBSTRING|substring)\((DATE_SUB|date_sub)\((NOW|now)\(\)\,(INTERVAL|interval)(.*)(MONTH|month)\)\,1\,11\)")
    result = date_sub_patten.sub(change_cur_date_sub_month_date, document_content)
    document_content = result

    # date_sub(now(),interval 30 MINUTE)
    date_sub_patten = re.compile(r"(DATE_SUB|date_sub)\((NOW|now)\(\)\,(INTERVAL|interval)(.*)(MINUTE|minute)\)")
    result = date_sub_patten.sub(change_date_sub_minute, document_content)
    document_content = result

    logging.info(" 格式化 date_sub 成功 ")
    return document_content
